<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('交易产品') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('债券一级类别')" prop="primaryBondCategory">
        <el-select v-model="queryParams.primaryBondCategory" :placeholder="$t('债券一级类别')" clearable @keyup.enter.native="queryHandle">
          <el-option
            v-for="dict in primaryBondCategoryOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('债券二级类别')" prop="secondaryBondCategory">
        <el-select v-model="queryParams.secondaryBondCategory" :placeholder="$t('债券二级类别')" clearable @keyup.enter.native="queryHandle">
          <el-option
            v-for="dict in secondaryBondCategoryOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('债券代码')" prop="bondCode">
        <el-input
          v-model="queryParams.bondCode"
          :placeholder="$t('债券代码')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('债券名称')" prop="bondName">
        <el-input
          v-model="queryParams.bondName"
          :placeholder="$t('债券名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:bondProduct:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['act:model:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportDealBaseBond"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dealBaseBondList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('债券一级类别')" align="center" prop="primaryBondCategory" :formatter="primaryBondCategoryFormat" />
      <el-table-column :label="$t('债券二级类别')" align="center" prop="secondaryBondCategory" :formatter="secondaryBondCategoryFormat" />
      <el-table-column :label="$t('债券代码')" align="center" prop="bondCode" />
      <el-table-column :label="$t('债券名称')" align="center" prop="bondName" />
      <!--
      <el-table-column :label="$t('融资主体')" align="center" prop="financingSubject" />
      <el-table-column :label="$t('债务人')" align="center" prop="obligor" />
      <el-table-column :label="$t('风险所在国')" align="center" prop="countryRisk" />
      <el-table-column :label="$t('行业一级')" align="center" prop="industryLevelOne" />
      <el-table-column :label="$t('行业二级')" align="center" prop="industryLevelTwo" />
      <el-table-column :label="$t('投资币种')" align="center" prop="currency" :formatter="currencyFormat" />
      <el-table-column :label="$t('到期日')" align="center" prop="dueDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dueDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('偿付顺序')" align="center" prop="repaymentOrder" :formatter="repaymentOrderFormat" />
      <el-table-column :label="$t('标的名称')" align="center" prop="subjectName" />
      <el-table-column :label="$t('系列')" align="center" prop="series" />
      <el-table-column :label="$t('级别')" align="center" prop="level" />
      <el-table-column :label="$t('结构化产品类别')" align="center" prop="structuredType" :formatter="structuredTypeFormat" />
      <el-table-column :label="$t('债券类型')" align="center" prop="bondType" />
      <el-table-column :label="$t('企业性质')" align="center" prop="natureEnterprise" :formatter="natureEnterpriseFormat" />
      <el-table-column :label="$t('债权名称')" align="center" prop="nameCreditorRight" />
      <el-table-column :label="$t('基金管理人')" align="center" prop="fundManager" />
      <el-table-column :label="$t('基金规模')" align="center" prop="fundSize" />
      <el-table-column :label="$t('投资期限')" align="center" prop="termInvestment" />
      <el-table-column :label="$t('发行人评级')" align="center" prop="externalRatingIssuer" :formatter="externalRatingIssuerFormat" />
      <el-table-column :label="$t('担保人名称')" align="center" prop="nameGuarantor" />
      <el-table-column :label="$t('担保人评级')" align="center" prop="ratingOutsideGuarantor" :formatter="ratingOutsideGuarantorFormat" />
      <el-table-column :label="$t('第三方委托人')" align="center" prop="thirdPartyPrincipal" />
      -->
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:bondProduct:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >
            {{ $t('修改') }}
          </el-button>
          <el-button
            v-hasPermi="['deal:bondProduct:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >
            {{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改交易产品对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealBaseBondForm ref="dealBaseBondForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealBaseBond, delDealBaseBond } from '@/api/bond/dealBondProduct/dealBondProduct.js'
import dealBaseBondForm from './dealBondProductForm'

export default {
  components: {
    dealBaseBondForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 交易产品表格数据
      dealBaseBondList: [],
      // 弹出层标题
      title: '交易产品',
      // 是否显示弹出层
      formOpen: false,
      // 债券一级类别字典
      primaryBondCategoryOptions: [],
      // 债券二级类别字典
      secondaryBondCategoryOptions: [],
      // 投资币种字典
      currencyOptions: [],
      // 偿付顺序字典
      repaymentOrderOptions: [],
      // 结构化产品类别字典
      structuredTypeOptions: [],
      // 企业性质字典
      natureEnterpriseOptions: [],
      // 发行人评级字典
      externalRatingIssuerOptions: [],
      // 担保人评级字典
      ratingOutsideGuarantorOptions: [],
      // 查询参数
      queryParams: {
        dealId: undefined,
        primaryBondCategory: undefined,
        secondaryBondCategory: undefined,
        bondCode: undefined,
        bondName: undefined,
        financingSubject: undefined,
        obligor: undefined,
        countryRisk: undefined,
        industryLevelOne: undefined,
        industryLevelTwo: undefined,
        currency: undefined,
        dueDate: undefined,
        repaymentOrder: undefined,
        subjectName: undefined,
        series: undefined,
        level: undefined,
        structuredType: undefined,
        bondType: undefined,
        natureEnterprise: undefined,
        nameCreditorRight: undefined,
        fundManager: undefined,
        fundSize: undefined,
        termInvestment: undefined,
        externalRatingIssuer: undefined,
        nameGuarantor: undefined,
        ratingOutsideGuarantor: undefined,
        thirdPartyPrincipal: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.getDicts('deal_bond_NW').then(response => {
      this.primaryBondCategoryOptions = response.data
    })
    this.getDicts('deal_bond_NW_ALL').then(response => {
      this.secondaryBondCategoryOptions = response.data
    })

    this.getDicts('currency').then(response => {
      this.currencyOptions = response.data
    })
    this.getDicts('deal_repayment_order').then(response => {
      this.repaymentOrderOptions = response.data
    })
    this.getDicts('deal_bond_structured_type').then(response => {
      this.structuredTypeOptions = response.data
    })
    this.getDicts('deal_bond_nature_enterprise').then(response => {
      this.natureEnterpriseOptions = response.data
    })
    this.getDicts('letter_rating').then(response => {
      this.externalRatingIssuerOptions = response.data
    })
    this.getDicts('letter_rating').then(response => {
      this.ratingOutsideGuarantorOptions = response.data
    })
  },
  methods: {
    /** 查询交易产品列表 */
    getList() {
      this.loading = true
      listDealBaseBond(this.queryParams).then(response => {
        this.dealBaseBondList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 债券一级类别字典翻译
    primaryBondCategoryFormat(row) {
      return this.selectDictLabel(this.primaryBondCategoryOptions, row.primaryBondCategory)
    },
    // 债券二级类别字典翻译
    secondaryBondCategoryFormat(row) {
      return this.selectDictLabel(this.secondaryBondCategoryOptions, row.secondaryBondCategory)
    },
    // 投资币种字典翻译
    currencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.currency)
    },
    // 偿付顺序字典翻译
    repaymentOrderFormat(row) {
      return this.selectDictLabel(this.repaymentOrderOptions, row.repaymentOrder)
    },
    // 结构化产品类别字典翻译
    structuredTypeFormat(row) {
      return this.selectDictLabel(this.structuredTypeOptions, row.structuredType)
    },
    // 企业性质字典翻译
    natureEnterpriseFormat(row) {
      return this.selectDictLabel(this.natureEnterpriseOptions, row.natureEnterprise)
    },
    // 发行人评级字典翻译
    externalRatingIssuerFormat(row) {
      return this.selectDictLabel(this.externalRatingIssuerOptions, row.externalRatingIssuer)
    },
    // 担保人评级字典翻译
    ratingOutsideGuarantorFormat(row) {
      return this.selectDictLabel(this.ratingOutsideGuarantorOptions, row.ratingOutsideGuarantor)
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        primaryBondCategory: undefined,
        secondaryBondCategory: undefined,
        bondCode: undefined,
        bondName: undefined,
        financingSubject: undefined,
        obligor: undefined,
        countryRisk: undefined,
        industryLevelOne: undefined,
        industryLevelTwo: undefined,
        currency: undefined,
        dueDate: undefined,
        repaymentOrder: undefined,
        subjectName: undefined,
        series: undefined,
        level: undefined,
        structuredType: undefined,
        bondType: undefined,
        natureEnterprise: undefined,
        nameCreditorRight: undefined,
        fundManager: undefined,
        fundSize: undefined,
        termInvestment: undefined,
        externalRatingIssuer: undefined,
        nameGuarantor: undefined,
        ratingOutsideGuarantor: undefined,
        thirdPartyPrincipal: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealBaseBondForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealBaseBondForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delDealBaseBond(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    exportDealBaseBond() {
      this.downLoadExcel('/dael/BondProduct/export', this.queryParams).then(() => {
        this.msgSuccess(this.$t('导出成功'))
      })
    }
  }
}
</script>
